package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.WorkOrder;
import com.jr.service.IWorkOrderService;
import com.jr.service.impl.WorkOrderServiceImpl;
import com.jr.util.PageHelper;
import jdk.nashorn.internal.ir.CallNode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

@WebServlet("/wo/*")
public class WorkOrderController extends HttpServlet {

    private IWorkOrderService iwos=new WorkOrderServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String substring = requestURI.substring(index);
        if (substring.length()==0){
            substring="showdt";
        }

        Class empControllerClass = this.getClass();
        Method method=null;

        try {
            method=empControllerClass.getMethod(substring,HttpServletRequest.class,HttpServletResponse.class);

            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");


            method.invoke(this,request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void add (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        /*"wo/add",{"executor":executor,
        "description":description,
                "orderlevel":orderlevel,
                "pid":pid*/

        String executor = request.getParameter("executor");
        String description = request.getParameter("description");
        Integer orderlevel =Integer.parseInt(request.getParameter("orderlevel")) ;
        Integer pid =Integer.parseInt(request.getParameter("pid")) ;

        /*Integer pid, String executor, String description, Integer orderlevel, Date createdate*/
        WorkOrder wo = new WorkOrder(pid,executor,description,orderlevel,new Date());
        boolean b = iwos.addWorkOrder(wo);
        response.getWriter().println(new Gson().toJson(b));
    }

    public void getListByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PageHelper ph =new PageHelper();
        int ndexpage = Integer.parseInt(request.getParameter("indexpage"));
        ph.setPageIndex(ndexpage);
        ph.setDateCount(iwos.queryPageDateCount());
        ph.setListIndex(iwos.queryIndexDate(ph));


        response.getWriter().println(new Gson().toJson(ph));

    }



}
